/**
 * author Creat
 */
package com.gcb.xml;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @author Creat
 * @Time 2011-4-1
 */
public class ConfigParser extends DefaultHandler {

	private SAXParserFactory factory = null;
	private SAXParser sp = null;
	private static HashMap<Object, Object> configMap = null;

	/**
	 * 
	 */
	public ConfigParser() {
		// TODO Auto-generated constructor stub
		factory = SAXParserFactory.newInstance();
		try {
			sp = factory.newSAXParser();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#endDocument()
	 */
	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
		System.out.println("结束解析");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
	 * java.lang.String, java.lang.String)
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#startDocument()
	 */
	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
		configMap = new HashMap<Object, Object>();
		System.out.println("开始解析");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
	 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attr) throws SAXException {
		// TODO Auto-generated method stub
		if ("config".equals(qName)) {
			configMap.put(attr.getValue(0), attr.getValue(1));
		}
	}

	public void parser(String filePath) {
		InputStream is = Class.class.getResourceAsStream(filePath);
		try {
			sp.parse(is, this);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * author Creat
	 * 
	 * @param args
	 */
	@SuppressWarnings("rawtypes")
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String filePath = "/com/gcb/xml/config.xml";
		ConfigParser cp = new ConfigParser();
		cp.parser(filePath);
		System.out.println(configMap.toString());
		Set set = configMap.entrySet();
		Iterator iter = set.iterator();
		while (iter.hasNext()) {
			Map.Entry me = (Entry) iter.next();
			String name = (String) me.getKey();
			String value = (String) me.getValue();
			System.out.println(name + " = " + value);
		}

	}

}
